package com.seecen.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.mapper.UserMapper;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.MD5Util;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 桃子
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-07-21 14:26:52
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getUserInfo(String token) {
        boolean isExpiration = jwtHelper.isExpiration(token);
        if(isExpiration){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        int userId = jwtHelper.getUserId(token).intValue();

        User user = userMapper.selectById(userId);
        //把密码设置为空
        user.setPassword("");

        HashMap map = new HashMap();
        map.put("loginUser",user);

        return Result.ok(map);
    }

    @Override
    public Result getSeekNameById(Integer id) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId,id);
        User user = userMapper.selectOne((wrapper));
        String username = user.getUsername();
        return Result.ok(username);
    }

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,user.getPhone());
        User loginUser = userMapper.selectOne((wrapper));

        if(loginUser == null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        if(!StringUtils.isEmpty(user.getPassword()) && MD5Util.encrypt(user.getPassword()).equals(loginUser.getPassword())){
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUserId()));
            HashMap map = new HashMap();
            map.put("token",token);
            return Result.ok(map);
        }
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,user.getPhone());
        Long count = userMapper.selectCount((wrapper));
        if(count > 0){
            return Result.build(null, ResultCodeEnum.PHONE_USED);
        }else{
            user.setPassword(MD5Util.encrypt(user.getPassword()));
            Integer ret = userMapper.insert(user);
            if(ret!=-1){
                return Result.build(null,ResultCodeEnum.SUCCESS);
            }
        }
        return Result.ok(user);
    }

}




